/* 
 * 
 * Created on 21 Oct 2011 by simonk
 * 
 * Copyright: Multicom Products Ltd. 2011 Bristol, England
 */
package objects.data;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.UUID;

import com.google.gson.Gson;

import objects.ObjectCodes;
import utils.Utils;
import utils.ZipUtils;

import gui.GUIUtils;
import gui.main.MainPanelGUIView;

/**
 *
 * @author simonk
 */
public class WorkPacket implements ObjectCodes {
	public String uniqueId;
	public String status;
	public String timestamp;
	public String jobname;
	public String sourceDir;
	public String resultDir;
	public String executable;
	public String args;
	public String walltime;
	public String pbsString;
	public int cluster;
	public int nodes;
	public int ppn;
	public int opt;
	public int optRange;
	public int replay;
	public boolean sendResultsBack;
	public boolean unzipResults;
	
	public WorkPacket() {	
		
	}
	
	public static WorkPacket getInstance() {
		
		MainPanelGUIView mainPanel = GUIUtils.MainPanel;
		if(mainPanel.jobNameTxt.getText().isEmpty()) {
			return null;
		}
		
		WorkPacket mpd = new WorkPacket();
		mpd.jobname = mainPanel.jobNameTxt.getText();
		mpd.uniqueId = mpd.jobname+"."+Utils.generateRandomString();
		mpd.sourceDir = mainPanel.jobDirTxt.getText();
		if(new File(mpd.sourceDir).isDirectory()) {
			mpd.sourceDir = ZipUtils.zip(mpd.sourceDir);
		}
		mpd.timestamp = new Date().toString();
		mpd.resultDir = mainPanel.resultsDirTxt.getText();
		mpd.executable = mainPanel.execTxt.getText();
		mpd.args = mainPanel.argsTxt.getText();
		mpd.walltime = mainPanel.wallTimeTxt.getText();
		mpd.cluster = mainPanel.assignRadio.isSelected() ? Utils.userInfo.getClusterId(mainPanel.clusterCombo.getSelectedItem().toString()) : -1;
		mpd.pbsString = mainPanel.PBSchk.isSelected() ? readPbsFile() : null;
		mpd.nodes = (Integer) mainPanel.nodesSpin.getValue();
		mpd.ppn = (Integer) mainPanel.ppnSpin.getValue();
		mpd.opt = mainPanel.optRadio.isSelected() ? mainPanel.optCombo.getSelectedIndex() : -1;
		mpd.optRange = (Integer) mainPanel.hoursSpin.getValue();
		mpd.replay = mainPanel.replayRadio.isSelected() ? (Integer) mainPanel.replaySpin.getValue() : 1;
		mpd.sendResultsBack = mainPanel.resultsBackChk.isSelected();
		mpd.unzipResults = mainPanel.unzipChk.isSelected();
		return mpd;
	}
	
	public static String readPbsFile() {
		MainPanelGUIView mainPanel = GUIUtils.MainPanel;
		String filename = mainPanel.PbsTxt.getText();
		if(filename.isEmpty()) {
			return null;
		}
		try{
			FileInputStream fstream = new FileInputStream(filename);
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String result = "";
			String strLine;
			while ((strLine = br.readLine()) != null)   {
				result += strLine+"\n";
			}
			return result;
		}catch(Exception e) {
			//ERROR MSG
			return null;
		}
	}

	public String toString()
	{
		return CLI_WORK_PKT +"|"+ new Gson().toJson(this);
	}
	
	public static WorkPacket fromJSon(String json)
	{
		return new Gson().fromJson(json,WorkPacket.class);
	}

}
